package Actions;

import Beans.Relationship;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.restassured.response.Response;
import org.omg.CORBA.OBJ_ADAPTER;
import org.testng.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static io.restassured.RestAssured.given;

public class SMSActions extends BaseAction {
    public Integer logisticsmode;
    public void loginSMS() {
        response = given()
                .contentType("application/x-www-form-urlencoded")
                .body("username=admin&password=123456a")
                .post("http://hc-t1.yonghuivip.com/signin");

        Assert.assertEquals(response.getStatusCode(), 302);
        log.info("登录成功！");
        cookies = response.getCookies();
    }

    public void loginSMS(String username, String pwd) {
        response = given()
                .contentType("application/x-www-form-urlencoded")
                .body("username=" + username + "&password=" + pwd + "")
                .post("http://hc-t1.yonghuivip.com/signin");
        Assert.assertEquals(response.getStatusCode(), 302);
        log.info("登录成功！");
        cookies = response.getCookies();
    }

    //获取采购单信息
    public Response getPOInfo(String pono) {
        response = given()
                .cookies(cookies)
                .queryParams("pagesize", "50", "pageindex", "1")
                .contentType("application/json")
                .body("{\n" +
                        "\t\"poid\": \"" + pono + "\"\n" +
                        "}")
                .post(URL + "sms-hub/purch/list");

        return response;
    }

    //获取采购单状态
    public String getPOStatus(Response res) {
        String status = res.jsonPath().getString("data.rows[0].statusname");

        return status;
    }

    //获取转移单信息
    public Response getTFInfo(String tfno) {
        response = given()
                .cookies(cookies)
                .queryParams("pagesize", "50", "pageindex", "1")
                .contentType("application/json")
                .body("{\n" +
                        "\t\"transid\": \"" + tfno + "\"\n" +
                        "}")
                .post(URL + "sms-hub/trans/list");

        return response;
    }

    //获取转移单状态
    public String getTFStatus(Response res) {
        String status = res.jsonPath().getString("data.rows[0].statusname");
        return status;
    }

    //获取需求单信息
    public Response getPRInfo(String shoptype, String billno) {
        char x = billno.charAt(1);
        String billtype;//需求单号、采购单号、转移单号
        switch (x) {
            case 'O':
                billtype = "purchid";
                break;
            case 'F':
                billtype = "transid";
                break;
            case 'R':
                billtype = "prid";
                break;
            default:
                billtype = "";
        }

        response = given()
                .cookies(cookies)
                .contentType("application/json")
                .queryParams("pagesize", "50", "pageindex", "1")
                .body("{\n" +
                        "\t\"storetypeid\": \"" + shoptype + "\",\n" +
                        "\t\"" + billtype + "\": \"" + billno + "\"\n" +
                        "}")
                .post(URL + "sms-hub/inquireshoporder/orderlist");
        return response;

    }

    //获取需求单状态
    public String getPRStatus(Response res) {
        String status = res.jsonPath().getString("data.rows[0].statusname");
        return status;
    }

    public Response createPO(String type, String shopcode, String skucode, Integer number) {
        Relationship rl=getRelationship(shopcode,skucode);
        Response r2 = findskushop(shopcode, skucode);
        String pogroupcode = r2.jsonPath().getString("data.rows[0].groupcode");
        String barcode = r2.jsonPath().getString("data.rows[0].barcode");
        String taxation = findTaxation(rl.getRatecode()).jsonPath().getString("data.rows[0].taxrate");

        Double amount = Double.valueOf(rl.getUnitpriceaftertax()) * number;
        Double purchaseamountnotax = Double.valueOf(rl.getUnitpricebeforetax()) * number;
        String poparame = "{\n" +
                "\t\"potype\": \"" + type + "\",\n" +
                "\t\"addresscode\": \"" + shopcode + "\",\n" +
                "\t\"pogroupcode\": \"" + pogroupcode + "\",\n" +
                "\t\"poorganizationcode\": \"" + rl.getPurchasecode() + "\",\n" +
                "\t\"providercode\": \"" + rl.getProvidercode() + "\",\n" +
                "\t\"addresssettlecode\": \"" + rl.getAddresssettlecode() + "\",\n" +
                "\t\"billlist\": [{\n" +
                "\t\t\"maxcategorycode\": \"" + rl.getMaxcategorycode() + "\",\n" +
                "\t\t\"purchasepricenotax\": " + rl.getUnitpricebeforetax() + ",\n" +
                "\t\t\"categorycode\": \"" + rl.getDepartmentcategorycode() + "\",\n" +
                "\t\t\"goodsname\": \"" + rl.getSkuname() + "\",\n" +
                "\t\t\"isconsortium\": false,\n" +
                "\t\t\"goodsbarcode\": \"" + barcode + "\",\n" +
                "\t\t\"rate\": " + taxation + ",\n" +
                "\t\t\"unitname\": \"" + rl.getUnitname() + "\",\n" +
                "\t\t\"price\": " + rl.getUnitpriceaftertax()+ ",\n" +
                "\t\t\"purchaseamountnotax\": " + purchaseamountnotax + ",\n" +
                "\t\t\"midcategoryname\": \"" + rl.getMiddlecategoryname() + "\",\n" +
                "\t\t\"amount\": " + amount.toString() + ",\n" +
                "\t\t\"mincategorycode\": \"" + rl.getSmallcategorycode() + "\",\n" +
                "\t\t\"quantity\": " + number.toString() + ",\n" +
                "\t\t\"goodsid\": \"" + skucode + "\",\n" +
                "\t\t\"packnum\": 5,\n" +
                "\t\t\"specification\": \"" + rl.getSpec() + "\",\n" +
                "\t\t\"isgift\": false,\n" +
                "\t\t\"modifyprice\": false,\n" +
                "\t\t\"unit\": \"" + rl.getUnitcode() + "\",\n" +
                "\t\t\"midcategorycode\": \"" + rl.getMiddlecategorycode() + "\",\n" +
                "\t\t\"maxcategoryname\": \"" + rl.getMaxcategoryname()+ "\",\n" +
                "\t\t\"categoryname\": \"" + rl.getDepartmentcategoryname() + "\",\n" +
                "\t\t\"ratecode\": \"" + rl.getRatecode() + "\",\n" +
                "\t\t\"mincategoryname\": \"" + rl.getSmallcategoryname() + "\",\n" +
                "\t\t\"rownum\": 1\n" +
                "\t}],\n" +
                "\"partnerlist\": []"+
                "}";

        //System.out.println(poparame);
        response = given()
                .contentType("application/json")
                .cookies(cookies)
                .body(poparame)
                .post(URL + "sms-hub/purch/create");
        resString = response.asString();
        log.info(resString);
        Assert.assertTrue(resString.contains("PO"), "创建采购单失败！！");
        log.info("创建采购单成功！！采购单号：" + resString);
        return response;
    }

    //审批单据 转移单、采购单、需求单
    public Response approveBills(String billno) {
        Character type = billno.charAt(1);

        response = null;
        String status = null;
        switch (type) {
            case 'O':
                response=getPOInfo(billno);
                String potype=response.jsonPath().getString("data.rows[0].potype");
                response = given()
                        .contentType("application/json")
                        .cookies(cookies)
                        .queryParams("poid", billno)
                        .post(URL + "sms-hub/purch/approve");
                Assert.assertTrue(response.asString().contains("\"code\":0"), "审批采购单失败！！");
                log.info("采购单：" + billno + "审批成功！");
                status = getPOStatus(getPOInfo(billno));

                    if("1".equals(potype)) {
                        Assert.assertEquals(status, "收货中", "采购单状态不正确!");
                    }else if ("2".equals(potype)) {
                        Assert.assertEquals(status, "已审批", "采购单状态不正确!");
                    }

                log.info("采购单状态:" + status);
                break;
            case 'F':
                response = given()
                        .cookies(cookies)
                        .param("transid", billno)
                        .post(URL + "sms-hub/trans/approve");
                Assert.assertTrue(response.asString().contains("0"), "审批转移单失败！");
                log.info("转移单" + billno + "审批成功！");
                status = getTFStatus(getTFInfo(billno));
                log.info(response.asString());
                Assert.assertEquals(status, "已审批", "转移单状态不正确!");
                log.info("采购单状态:" + status);
                break;
        }
        return response;
    }

    public Response approvePR(String billno){

        //提数
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .body("{\n" +
                        "\t\"storetypeid\": \"1\",\n" +
                        "\t\"logistics\": \""+logisticsmode+"\",\n" +
                        "\t\"prid\": \""+billno+"\"\n" +
                        "}")
                .post(URL+"sms-hub/iapproveshoporder/orderextract");
        String batchid=response.asString();
        //System.out.println(batchid);

        //将订单放入审批列表(暂审)
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .queryParams("batchid",batchid,"isall",true,"logistics",logisticsmode)
                .post(URL+"sms-hub/iapproveshoporder/tempapprove");
       // System.out.println(response.asString());
        //审批列表中的订单
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .queryParams("batchid",batchid,"isall",true,"logistics",logisticsmode)
                .post(URL+"sms-hub/iapproveshoporder/approve");

        response=getPRInfo("1",billno);
        return response;
    }

    //获取单据信息
    public Response getBillInfo(String billno) {
        Character type = billno.charAt(1);
        response = null;
        switch (type) {
            case 'O':
                response = getPOInfo(billno);
                break;
            case 'F':
                response = getTFInfo(billno);
                break;
            case 'R':

                break;
            case 'H':
                break;
            case 'k':
                break;
        }

        return response;
    }

    //获取单据状态
    public String getBillStatus(Response res) {
        return res.jsonPath().getString("data.rows[0].statusname");
    }

    //创建需求单
    public Response createPR(String shopcode,String skucode,Integer skunumber) {

        Relationship rl=getRelationship(shopcode,skucode);
        logisticsmode= Integer.valueOf(rl.getLogisticsmode());

        switch (logisticsmode){
            case 1:log.info("门店"+shopcode+"下商品"+skucode+"物流模式为直送");break;
            case 2:log.info("门店"+shopcode+"下商品"+skucode+"物流模式为配送");break;
            case 3:log.info("门店"+shopcode+"下商品"+skucode+"物流模式为货到既配");break;
            case 4:log.info("门店"+shopcode+"下商品"+skucode+"物流模式为直通");break;
        }
        response = given()
                .cookies(cookies)
                .queryParam("shopid", shopcode)
                .contentType("application/json")
                .body("[{\n" +
                        "\t\"skucode\": \""+skucode+"\",\n" +
                        "\t\"suppliercode\": \""+rl.getProvidercode()+"\",\n" +
                        "\t\"shopid\": \""+skucode+"\",\n" +
                        "\t\"orderqty\": "+skunumber.toString()+",\n" +
                        "\t\"rownum\": 1\n" +
                        "}]")
                .post(URL + "sms-hub/importshoporder/order");

        return response;


    }

    private Response createZS(String shopcode, String skucode, Integer skunumber) {
        Relationship rl=getRelationship(shopcode,skucode);
        response = given()
                .cookies(cookies)
                .queryParam("shopid", shopcode)
                .contentType("application/json")
                .body("[{\n" +
                        "\t\"occupationamount\": 80392.1,\n" +
                        "\t\"logisticsname\": \"直送\",\n" +
                        "\t\"nowamount\": 2108575,\n" +
                        "\t\"actualstock\": 2108575,\n" +
                        "\t\"depotstock\": -9953,\n" +
                        "\t\"inprice\": 10,\n" +
                        "\t\"skucode\": \""+skucode+"\",\n" +
                        "\t\"saleprice\": 15,\n" +
                        "\t\"specification\": \"*\",\n" +
                        "\t\"onwayamount\": 377,\n" +
                        "\t\"skuname\": \"纸皮核桃\",\n" +
                        "\t\"suppliername\": \"上海福庚贸易有限公司\",\n" +
                        "\t\"regionskuname\": \"纸皮核桃null\",\n" +
                        "\t\"unitname\": \"包\",\n" +
                        "\t\"logisticsstatus\": false,\n" +
                        "\t\"suppliercode\": \""+rl.getProvidercode()+"\",\n" +
                        "\t\"shopid\": \""+skucode+"\",\n" +
                        "\t\"orderqty\": "+skunumber.toString()+",\n" +
                        "\t\"rownum\": 1\n" +
                        "}]")
                .post(URL + "sms-hub/importshoporder/order");
        System.out.println(1);
        return response;
    }


    public Response createTF(String outaddress,String inaddress,String skucode,Integer skunumber) {
        Relationship rl=getRelationship(inaddress,skucode);

        response = given()
                .cookies(cookies)
                .contentType("application/json")
                .body("{\n" +
                        "    \"addressincode\":\""+inaddress+"\",\n" +
                        "    \"transferrelation\":\"1\",\n" +
                        "    \"addressoutcode\":\""+outaddress+"\",\n" +
                        "    \"isnearexpire\":false,\n" +
                        "    \"billlist\":[\n" +
                        "        {\n" +
                        "            \"maxcategorycode\":\""+rl.getMaxcategorycode()+"\",\n" +
                        "            \"categorycode\":\""+rl.getDepartmentcategorycode()+"\",\n" +
                        "            \"goodsname\":\""+rl.getSkuname()+"\",\n" +
                        "            \"isconsortium\":false,\n" +
                        "            \"isavgprice\":true,\n" +
                        "            \"goodsbarcode\":\"6901939651605X\",\n" +
                        "            \"providercode\":\"20020260\",\n" +
                        "            \"rate\":16,\n" +
                        "            \"unitname\":\"瓶\",\n" +
                        "            \"price\":1303.7,\n" +
                        "            \"amountnotax\":1123.88,\n" +
                        "            \"midcategoryname\":\"碳酸饮料\",\n" +
                        "            \"amount\":1303.7,\n" +
                        "            \"mincategorycode\":\"12300101\",\n" +
                        "            \"quantity\":"+skunumber+",\n" +
                        "            \"classgroup\":\"A01\",\n" +
                        "            \"goodsid\":\"101963\",\n" +
                        "            \"packnum\":5,\n" +
                        "            \"specification\":\"600ml\",\n" +
                        "            \"modifyprice\":false,\n" +
                        "            \"unit\":\"18\",\n" +
                        "            \"midcategorycode\":\"123001\",\n" +
                        "            \"maxcategoryname\":\"软性饮料\",\n" +
                        "            \"pricenotax\":1123.88,\n" +
                        "            \"categoryname\":\"食品类\",\n" +
                        "            \"mincategoryname\":\"可乐\",\n" +
                        "            \"ratecode\":\"XB\",\n" +
                        "            \"rownum\":1\n" +
                        "        }\n" +
                        "    ],\n" +
                        "    \"partnerlist\": []\n" +  //[ "+ JSON.toJSONString(getListPartner(inaddress,outaddress,"P300"))+"]
                        "    "+
                        "}")
                .post(URL + "sms-hub/trans/create");

        Assert.assertNotEquals("", response.asString(), "创建转移单失败！");
        log.info("新建转移单成功！转移单号为：" + response.asString());
        return response;

    }

    //获取转移单中的合作伙伴 转入、转出、采购组织
    public List<Object> getListPartner(String shopin, String shopout, String purchaOrg){
       List<Object> listPartner;
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .queryParams("providercode","S"+shopout,"poorganizationcode",purchaOrg,"location","S"+shopin)
                .post(URL+"sms-hub/common/listpartner");
         listPartner=response.jsonPath().getList("data");
        return listPartner;
    }

    //获取交付开票信息
    public String getInvoice(String shopin, String shopout, String purchaOrg){
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .queryParams("providercode","S"+shopout,"poorganizationcode",purchaOrg,"addresscode",shopin)
                .post(URL+"sms-hub/common/getinvoice");
        System.out.println(response.asString());
        String invoice=response.jsonPath().getString("data");

        return invoice;
    }




    public Response getTFPrice(String outaddress,String inaddress,String skucode){
        response=given()
                .contentType("application/json")
                .cookies(cookies)
                .queryParams("addressincode",inaddress,"addressoutcode",outaddress,"goodsid",skucode,"issalesreturn",false)
                .post(URL+"sms-hub/trans/gettransprice");
        return  response;
    }
    public Response getTFBilldata(){return  null;}



}